package tmp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class BrowserClass {
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Browser Example");
		shell.setSize(620, 500);

		ToolBar toolbar = new ToolBar(shell, SWT.NONE);
		toolbar.setBounds(5, 5, 200, 30);

		ToolItem goButton = new ToolItem(toolbar, SWT.PUSH);
		goButton.setText("Go");

		ToolItem backButton = new ToolItem(toolbar, SWT.PUSH);
		backButton.setText("Back");

		ToolItem stopButton = new ToolItem(toolbar, SWT.PUSH);
		stopButton.setText("Stop");
		
		ToolItem blehButton = new ToolItem(toolbar, SWT.PUSH);
		blehButton.setText("Bleh");
		
		final Text text = new Text(shell, SWT.BORDER);
		text.setBounds(5, 35, 400, 25);

		final Browser browser = new Browser(shell, SWT.NONE);
		browser.setBounds(5, 75, 600, 400);

		Listener listener = new Listener() {
			public void handleEvent(Event event) {
				ToolItem item = (ToolItem) event.widget;
				String string = item.getText();
				
				if (string.equals("Back"))
					browser.back();
				else if (string.equals("Stop"))
					browser.stop();
				else if (string.equals("Go"))
					browser.setUrl(text.getText());
				else if (string.equalsIgnoreCase("Bleh"))
					System.out.println(browser.getText());
			}
		};

		goButton.addListener(SWT.Selection, listener);
		backButton.addListener(SWT.Selection, listener);
		stopButton.addListener(SWT.Selection, listener);
		blehButton.addListener(SWT.Selection, listener);

		text.addListener(SWT.DefaultSelection, new Listener() {
			public void handleEvent(Event e) {
				browser.setUrl(text.getText());
			}
		});
		
		browser.addStatusTextListener(new StatusTextListener() {
			@Override
			public void changed(StatusTextEvent event) {
				if (event.text.equalsIgnoreCase("done")) {
					Browser b = (Browser) event.getSource();
					System.out.println(b.getText());
				}
			}
		});
		shell.open();
		browser.setUrl("http://www.google.com");
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}